Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankId: "",
    currentBankId: "",
    organizationIds: [],
    beBranchArr: [],
    searchValue: "",
    selectRadio: "",
    pageNum: 1,
    pageSize: 50,
    type: '',
    listId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      type: options.type || '',
    })
    console.log(JSON.parse(options.organizationIds));
    if (JSON.parse(options.organizationIds)) {
      this.setData({
        organizationIds: JSON.parse(options.organizationIds),
        bankId: options.bankId,
        listId: options.listId
      })
    }
    // 获取所属分支行列表
    getApp().globalData.request(
      "/corpVisit/queryDescendantNodes", {
      bankName: this.data.searchValue,
      bankId: this.data.listId,
    },
      "POST"
    ).then(res => {
      console.log(res);
      this.setData({
        beBranchArr: res.data
      })

    })
  },

  valueChange (e) {
    if (e.detail.length == 0) {
      this.setData({
        beBranchArr: [],
        pageNum: 1
      })
    } else {
      this.setData({
        searchValue: e.detail
      })
    }
  },

  onChange (event) {
    var _this = this
    _this.setData({
      searchValue: event.detail,
      pageNum: 1
    })
    wx.showLoading({
      title: '搜索中',
    })
    const params = {
      bankName: this.data.searchValue,
      bankId: wx.getStorageSync('userInfo').highLevelBankId,
    }
    console.log(params, 'params')
    getApp().globalData.request(
      "/corpVisit/queryDescendantNodes", params,
      "POST"
    ).then(res => {
      console.log("++++++++", res);
      wx.hideLoading()
      _this.setData({
        beBranchArr: res.data
      })
    })

  },

  selectChange (e) {
    this.setData({
      selectRadio: e.detail,
      currentBankId: e.currentTarget.dataset.currentbankid
    });
  },
  confimClick () {
    if (this.data.selectRadio !== "") {
      let params = {
        "bankId": this.data.bankId,
        "corpCredits": this.data.organizationIds,
        "receivedBankId": this.data.currentBankId,
        "type": this.data.type || '1'
      }
      console.log(params);
      getApp().globalData.request(
        "/corpVisit/corpAllocToBank", params,
        "POST"
      ).then(res => {
        console.log(res);
        if (res.code == "200") {
          wx.showToast({
            title: '分配成功',
            icon: 'success'
          })
          setTimeout(() => {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
              refresh: true,
              list: [],
              pageNum: 1,
              isManage: false,
              ischecked: false,
            })
            wx.navigateBack({
              delta: 1 // 返回上一级页面。
            })
          }, 1000);

        } else {
          wx.showToast({
            title: '分配失败',
            icon: 'error'
          })
        }
      })
    } else {
      wx.showToast({
        title: '请先选择客户经理！',
        icon: "none"
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})